package com.usts.edu.blog.web.interceptor;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import com.usts.edu.blog.config.SystemVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Guanzhong Hu
 * Date :2020/4/14
 * Description : 订制前台拦截器
 * Version :1.0
 */
@Component
public class IndexInterceptor  implements HandlerInterceptor {

    @Resource
    private PolicyUtils.Commons commons;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        String requestType = request.getHeader("X-Requested-With");
        //非ajax请求
        if(!"XMLHttpRequest".equals(requestType)){
            // 工具类
            request.setAttribute("commons", commons);
            // 设置项
            request.setAttribute("options", SystemVariable.OPTIONS);
            // 菜单
            request.setAttribute("menus", SystemVariable.MENUS);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }

}
